package DAL;

import Entities.TblAcademic;
import DatabaseAccess.SQLHelper;
import java.sql.ResultSet;
import java.util.*;

public class AcademicDAL {

    private ResultSet objResult;
    public List<TblAcademic> list;

    public AcademicDAL() {
    }

    public boolean add(TblAcademic academic) {
        int result = 0;
        String[] params = new String[2];
        params[0] = academic.getStudentRollNo();
        params[1] = String.valueOf(academic.getClassId());

        try {
            result = SQLHelper.executeUpdate("ProcInsertAcademic", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean update(TblAcademic academic) {
        int result = 0;
        String[] params = new String[3];
        params[0] = String.valueOf(academic.getAcademicId());
        params[1] = academic.getStudentRollNo();
        params[2] = String.valueOf(academic.getClassId());

        try {
            result = SQLHelper.executeUpdate("procUpdateAcademic", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean delete(TblAcademic academic){
        int result = 0;
        String[] params = new String[1];
        params[0] = String.valueOf(academic.getClassId());

        try {
            result = SQLHelper.executeUpdate("procDeleteAcademic", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public List<TblAcademic> getAll() {
        list = new ArrayList<TblAcademic>();
        try {
            objResult = SQLHelper.executeQuery("procGetAllAcademic", null);
            while (objResult.next()) {
                TblAcademic academic = new TblAcademic();
                academic.setAcademicId(objResult.getInt("AcademicId"));
                academic.setStudentRollNo(objResult.getString("StudentRollNo"));
                academic.setClassId(objResult.getInt("ClassId"));
                academic.setJoinDate(objResult.getDate("JoinDate"));
                academic.setStatus(objResult.getBoolean("Status"));
                list.add(academic);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public TblAcademic getById(int id) {
        TblAcademic academic = new TblAcademic();
        String[] params = new String[1];
        params[0] = String.valueOf(id);
        try {
            objResult = SQLHelper.executeQuery("procGetAcademicById", params);
            while (objResult.next()) {
                academic.setAcademicId(objResult.getInt("AcademicId"));
                academic.setStudentRollNo(objResult.getString("StudentRollNo"));
                academic.setClassId(objResult.getInt("ClassId"));
                academic.setJoinDate(objResult.getDate("JoinDate"));
                academic.setStatus(objResult.getBoolean("Status"));
            }

            return academic;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
}
